﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.ComponentModel;
using System.Reflection;
using System.Collections;
using System.Windows.Forms;
using THSLib;
using THSERP.BusinessEntities;

namespace THSERP.BaseProvider
{
    partial class ERPModuleEntities
    {

        #region Set Default Values from Customer

        #region Get Contacts From Customer
        public virtual void SetSOContactFromCustomer(int iARCustomerID)
        {
            String strMainObjectTableName = THSUtil.GetTableNameFromBusinessObject(MainObject);

            ARCustomerContactsController objARCustomerContactsController = new ARCustomerContactsController();
            List<ARCustomerContactsInfo> lstARCustomerContactsInfo = objARCustomerContactsController.GetARCustomerContactsListByARCustomerIDAndARCustomerContactType(iARCustomerID, ADConfigValueUtility.cstContactTypeSaleOrder);
            
            //If exist Customer Contact with type is Sale Order, copy contact and address of first Customer Contact in list to SO Contact
            if(lstARCustomerContactsInfo.Count>0)
            {
                ARCustomerContactsInfo objARCustomerContactsInfo = lstARCustomerContactsInfo[0];
                String strCustomerContactTableName = THSUtil.GetTableNameFromBusinessObject(objARCustomerContactsInfo);

                PropertyInfo[] properties = MainObject.GetType().GetProperties();

                foreach (PropertyInfo prop in properties)
                {
                    if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "SOContact"))
                    {
                        String strCustomerContactPropertyName = strCustomerContactTableName.Substring(0, strCustomerContactTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 8);
                        PropertyInfo customerContactProperty = objARCustomerContactsInfo.GetType().GetProperty(strCustomerContactPropertyName);
                        if (customerContactProperty != null)
                        {
                            object objValue = customerContactProperty.GetValue(objARCustomerContactsInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }

                    if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "SOAddress"))
                    {
                        String strCustomerContactPropertyName = strCustomerContactTableName.Substring(0, strCustomerContactTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 1);
                        PropertyInfo customerContactProperty = objARCustomerContactsInfo.GetType().GetProperty(strCustomerContactPropertyName);
                        if (customerContactProperty != null)
                        {
                            object objValue = customerContactProperty.GetValue(objARCustomerContactsInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }
                }
            }
            //else, copy main contact and address of customer to SO Contact
            else
            {
                ARCustomersController objARCustomersController= new ARCustomersController();
                ARCustomersInfo objARCustomersInfo=(ARCustomersInfo)objARCustomersController.GetObjectByID(iARCustomerID);                
                if(objARCustomersInfo!=null)
                {
                    String strCustomerTableName = THSUtil.GetTableNameFromBusinessObject(objARCustomersInfo);

                    PropertyInfo[] properties = MainObject.GetType().GetProperties();

                    foreach (PropertyInfo prop in properties)
                    {
                        if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "SOContact"))
                        {
                            String strCustomerPropertyName = strCustomerTableName.Substring(0,strCustomerTableName.Length-1) + prop.Name.Substring(strMainObjectTableName.Length + 1);
                            PropertyInfo customerProperty = objARCustomersInfo.GetType().GetProperty(strCustomerPropertyName);
                            if (customerProperty != null)
                            {
                                object objValue = customerProperty.GetValue(objARCustomersInfo, null);
                                prop.SetValue(MainObject, objValue, null);
                            }
                        }

                        if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "SOAddress"))
                        {
                            String strCustomerPropertyName = strCustomerTableName.Substring(0, strCustomerTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 1);
                            PropertyInfo customerProperty = objARCustomersInfo.GetType().GetProperty(strCustomerPropertyName);
                            if (customerProperty != null)
                            {
                                object objValue = customerProperty.GetValue(objARCustomersInfo, null);
                                prop.SetValue(MainObject, objValue, null);
                            }
                        }
                    }
                }
            }           

        }

        public void SetDeliveryContactFromCustomer(int iARCustomerID)
        {
            String strMainObjectTableName = THSUtil.GetTableNameFromBusinessObject(MainObject);

            ARCustomerContactsController objARCustomerContactsController = new ARCustomerContactsController();
            List<ARCustomerContactsInfo> lstARCustomerContactsInfo = objARCustomerContactsController.GetARCustomerContactsListByARCustomerIDAndARCustomerContactType(iARCustomerID, ADConfigValueUtility.cstContactTypeDelivery);

            //If exist Customer Contact with type is Sale Order, copy contact and address of first Customer Contact in list to SO Contact
            if (lstARCustomerContactsInfo.Count > 0)
            {
                ARCustomerContactsInfo objARCustomerContactsInfo = lstARCustomerContactsInfo[0];
                String strCustomerContactTableName = THSUtil.GetTableNameFromBusinessObject(objARCustomerContactsInfo);

                PropertyInfo[] properties = MainObject.GetType().GetProperties();

                foreach (PropertyInfo prop in properties)
                {
                    if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "DeliveryContact"))
                    {
                        String strCustomerContactPropertyName = strCustomerContactTableName.Substring(0, strCustomerContactTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 14);
                        PropertyInfo customerContactProperty = objARCustomerContactsInfo.GetType().GetProperty(strCustomerContactPropertyName);
                        if (customerContactProperty != null)
                        {
                            object objValue = customerContactProperty.GetValue(objARCustomerContactsInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }

                    if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "DeliveryAddress"))
                    {
                        String strCustomerContactPropertyName = strCustomerContactTableName.Substring(0, strCustomerContactTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 7);
                        PropertyInfo customerContactProperty = objARCustomerContactsInfo.GetType().GetProperty(strCustomerContactPropertyName);
                        if (customerContactProperty != null)
                        {
                            object objValue = customerContactProperty.GetValue(objARCustomerContactsInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }
                }
            }
            //else, copy main contact and address of customer to SO Contact
            else
            {
                ARCustomersController objARCustomersController = new ARCustomersController();
                ARCustomersInfo objARCustomersInfo = (ARCustomersInfo)objARCustomersController.GetObjectByID(iARCustomerID);
                if (objARCustomersInfo != null)
                {
                    String strCustomerTableName = THSUtil.GetTableNameFromBusinessObject(objARCustomersInfo);

                    PropertyInfo[] properties = MainObject.GetType().GetProperties();

                    foreach (PropertyInfo prop in properties)
                    {
                        if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "DeliveryContact"))
                        {
                            String strCustomerPropertyName = strCustomerTableName.Substring(0, strCustomerTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 7);
                            PropertyInfo customerProperty = objARCustomersInfo.GetType().GetProperty(strCustomerPropertyName);
                            if (customerProperty != null)
                            {
                                object objValue = customerProperty.GetValue(objARCustomersInfo, null);
                                prop.SetValue(MainObject, objValue, null);
                            }
                        }

                        if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "DeliveryAddress"))
                        {
                            String strCustomerPropertyName = strCustomerTableName.Substring(0, strCustomerTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 7);
                            PropertyInfo customerProperty = objARCustomersInfo.GetType().GetProperty(strCustomerPropertyName);
                            if (customerProperty != null)
                            {
                                object objValue = customerProperty.GetValue(objARCustomersInfo, null);
                                prop.SetValue(MainObject, objValue, null);
                            }
                        }
                    }
                }
            }
        }

        public void SetInvoiceContactFromCustomer(int iARCustomerID)
        {

            String strMainObjectTableName = THSUtil.GetTableNameFromBusinessObject(MainObject);

            ARCustomerContactsController objARCustomerContactsController = new ARCustomerContactsController();
            List<ARCustomerContactsInfo> lstARCustomerContactsInfo = objARCustomerContactsController.GetARCustomerContactsListByARCustomerIDAndARCustomerContactType(iARCustomerID, ADConfigValueUtility.cstContactTypeInvoice);

            //If exist Customer Contact with type is Sale Order, copy contact and address of first Customer Contact in list to SO Contact
            if (lstARCustomerContactsInfo.Count > 0)
            {
                ARCustomerContactsInfo objARCustomerContactsInfo = lstARCustomerContactsInfo[0];
                String strCustomerContactTableName = THSUtil.GetTableNameFromBusinessObject(objARCustomerContactsInfo);

                PropertyInfo[] properties = MainObject.GetType().GetProperties();

                foreach (PropertyInfo prop in properties)
                {
                    if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "InvoiceContact"))
                    {
                        String strCustomerContactPropertyName = strCustomerContactTableName.Substring(0, strCustomerContactTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 13);
                        PropertyInfo customerContactProperty = objARCustomerContactsInfo.GetType().GetProperty(strCustomerContactPropertyName);
                        if (customerContactProperty != null)
                        {
                            object objValue = customerContactProperty.GetValue(objARCustomerContactsInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }

                    if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "SOAddress"))
                    {
                        String strCustomerContactPropertyName = strCustomerContactTableName.Substring(0, strCustomerContactTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 6);
                        PropertyInfo customerContactProperty = objARCustomerContactsInfo.GetType().GetProperty(strCustomerContactPropertyName);
                        if (customerContactProperty != null)
                        {
                            object objValue = customerContactProperty.GetValue(objARCustomerContactsInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }
                }
            }
            //else, copy main contact and address of customer to SO Contact
            else
            {
                ARCustomersController objARCustomersController = new ARCustomersController();
                ARCustomersInfo objARCustomersInfo = (ARCustomersInfo)objARCustomersController.GetObjectByID(iARCustomerID);
                if (objARCustomersInfo != null)
                {
                    String strCustomerTableName = THSUtil.GetTableNameFromBusinessObject(objARCustomersInfo);

                    PropertyInfo[] properties = MainObject.GetType().GetProperties();

                    foreach (PropertyInfo prop in properties)
                    {
                        if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "InvoiceContact"))
                        {
                            String strCustomerPropertyName = strCustomerTableName.Substring(0, strCustomerTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 6);
                            PropertyInfo customerProperty = objARCustomersInfo.GetType().GetProperty(strCustomerPropertyName);
                            if (customerProperty != null)
                            {
                                object objValue = customerProperty.GetValue(objARCustomersInfo, null);
                                prop.SetValue(MainObject, objValue, null);
                            }
                        }

                        if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "InvoiceAddress"))
                        {
                            String strCustomerPropertyName = strCustomerTableName.Substring(0, strCustomerTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 6);
                            PropertyInfo customerProperty = objARCustomersInfo.GetType().GetProperty(strCustomerPropertyName);
                            if (customerProperty != null)
                            {
                                object objValue = customerProperty.GetValue(objARCustomersInfo, null);
                                prop.SetValue(MainObject, objValue, null);
                            }
                        }
                    }
                }
            }
        }
        #endregion

        public virtual void SetDefaultValuesFromCustomer()
        {
           
            String strMainObjectName = THSUtil.GetTableNameFromBusinessObject(MainObject);
            String strMainObjectPaymentTermPropertyName = strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "PaymentTerm";
            String strMainObjectTableNamePrefix = strMainObjectName.Substring(0, 2);

            int iARCustomerID = Convert.ToInt32(THSDbUtil.GetPropertyValue(MainObject, "FK_ARCustomerID"));

            if (iARCustomerID > 0)
            {
                ARCustomersInfo objARCustomersInfo = (ARCustomersInfo)new ARCustomersController().GetObjectByID(iARCustomerID);
                if (objARCustomersInfo != null)
                {
                    
                    SetSOContactFromCustomer(iARCustomerID);
                    SetDeliveryContactFromCustomer(iARCustomerID);
                    SetInvoiceContactFromCustomer(iARCustomerID);


                    THSDbUtil.SetPropertyValue(MainObject, String.Format("{0}SellPriceCombo",strMainObjectTableNamePrefix), objARCustomersInfo.ARSellPriceCombo);

                    THSDbUtil.SetPropertyValue(MainObject, String.Format("{0}PaymentMethodCombo",strMainObjectTableNamePrefix), objARCustomersInfo.ARPaymentMethodCombo);

                    THSDbUtil.SetPropertyValue(MainObject, strMainObjectPaymentTermPropertyName, objARCustomersInfo.ARCustomerPaymentTerm);

                    THSDbUtil.SetPropertyValue(MainObject, "FK_ARSellerID", objARCustomersInfo.FK_ARSellerIDCombo);

                    THSDbUtil.SetPropertyValue(MainObject, "FK_GELanguageID", objARCustomersInfo.FK_GELanguageIDCombo);

                    THSDbUtil.SetPropertyValue(MainObject, "FK_GECurrencyID", objARCustomersInfo.FK_GECurrencyIDCombo);
                }
                UpdateMainObjectBindingSource();

            }
        }
        #endregion

       
        #region Set Default Values from Supplier

        #region Get Contacts from Supplier
        public virtual void SetPOContactFromSupplier(int iAPSupplierID)
        {
            String strMainObjectTableName = THSUtil.GetTableNameFromBusinessObject(MainObject);

            APSupplierContactsController objAPSupplierContactsController = new APSupplierContactsController();
            List<APSupplierContactsInfo> lstAPSupplierContactsInfo = objAPSupplierContactsController.GetAPSupplierContactsListByAPSupplierIDAndAPSupplierContactType(iAPSupplierID, ADConfigValueUtility.cstContactTypePurchaseOrder);

            //If exist Supplier Contact with type is Purchase Order, copy contact and address of first Supplier Contact in list to PO Contact
            if (lstAPSupplierContactsInfo.Count > 0)
            {
                APSupplierContactsInfo objAPSupplierContactsInfo = lstAPSupplierContactsInfo[0];
                String strSupplierContactTableName = THSUtil.GetTableNameFromBusinessObject(objAPSupplierContactsInfo);

                PropertyInfo[] properties = MainObject.GetType().GetProperties();

                foreach (PropertyInfo prop in properties)
                {
                    if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "POContact"))
                    {
                        String strSupplierContactPropertyName = strSupplierContactTableName.Substring(0, strSupplierContactTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 8);
                        PropertyInfo supplierContactProperty = objAPSupplierContactsInfo.GetType().GetProperty(strSupplierContactPropertyName);
                        if (supplierContactProperty != null)
                        {
                            object objValue = supplierContactProperty.GetValue(objAPSupplierContactsInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }

                    if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "POAddress"))
                    {
                        String strSupplierContactPropertyName = strSupplierContactTableName.Substring(0, strSupplierContactTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 1);
                        PropertyInfo supplierContactProperty = objAPSupplierContactsInfo.GetType().GetProperty(strSupplierContactPropertyName);
                        if (supplierContactProperty != null)
                        {
                            object objValue = supplierContactProperty.GetValue(objAPSupplierContactsInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }
                }
            }
            //else, copy main contact and address of supplier to PO Contact
            else
            {
                APSuppliersController objAPSuppliersController = new APSuppliersController();
                APSuppliersInfo objAPSuppliersInfo = (APSuppliersInfo)objAPSuppliersController.GetObjectByID(iAPSupplierID);
                if (objAPSuppliersInfo != null)
                {
                    String strSupplierTableName = THSUtil.GetTableNameFromBusinessObject(objAPSuppliersInfo);

                    PropertyInfo[] properties = MainObject.GetType().GetProperties();

                    foreach (PropertyInfo prop in properties)
                    {
                        if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "POContact"))
                        {
                            String strSupplierPropertyName = strSupplierTableName.Substring(0, strSupplierTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 1);
                            PropertyInfo supplierProperty = objAPSuppliersInfo.GetType().GetProperty(strSupplierPropertyName);
                            if (supplierProperty != null)
                            {
                                object objValue = supplierProperty.GetValue(objAPSuppliersInfo, null);
                                prop.SetValue(MainObject, objValue, null);
                            }
                        }

                        if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "POAddress"))
                        {
                            String strSupplierPropertyName = strSupplierTableName.Substring(0, strSupplierTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 1);
                            PropertyInfo supplierProperty = objAPSuppliersInfo.GetType().GetProperty(strSupplierPropertyName);
                            if (supplierProperty != null)
                            {
                                object objValue = supplierProperty.GetValue(objAPSuppliersInfo, null);
                                prop.SetValue(MainObject, objValue, null);
                            }
                        }
                    }
                }
            }

        }

        public void SetDeliveryContactFromSupplier(int iAPSupplierID)
        {
            String strMainObjectTableName = THSUtil.GetTableNameFromBusinessObject(MainObject);

            APSupplierContactsController objAPSupplierContactsController = new APSupplierContactsController();
            List<APSupplierContactsInfo> lstAPSupplierContactsInfo = objAPSupplierContactsController.GetAPSupplierContactsListByAPSupplierIDAndAPSupplierContactType(iAPSupplierID, ADConfigValueUtility.cstContactTypeDelivery);

            //If exist Supplier Contact with type is Purchase Order, copy contact and address of first Supplier Contact in list to PO Contact
            if (lstAPSupplierContactsInfo.Count > 0)
            {
                APSupplierContactsInfo objAPSupplierContactsInfo = lstAPSupplierContactsInfo[0];
                String strSupplierContactTableName = THSUtil.GetTableNameFromBusinessObject(objAPSupplierContactsInfo);

                PropertyInfo[] properties = MainObject.GetType().GetProperties();

                foreach (PropertyInfo prop in properties)
                {
                    if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "DeliveryContact"))
                    {
                        String strSupplierContactPropertyName = strSupplierContactTableName.Substring(0, strSupplierContactTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 14);
                        PropertyInfo supplierContactProperty = objAPSupplierContactsInfo.GetType().GetProperty(strSupplierContactPropertyName);
                        if (supplierContactProperty != null)
                        {
                            object objValue = supplierContactProperty.GetValue(objAPSupplierContactsInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }

                    if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "DeliveryAddress"))
                    {
                        String strSupplierContactPropertyName = strSupplierContactTableName.Substring(0, strSupplierContactTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 7);
                        PropertyInfo supplierContactProperty = objAPSupplierContactsInfo.GetType().GetProperty(strSupplierContactPropertyName);
                        if (supplierContactProperty != null)
                        {
                            object objValue = supplierContactProperty.GetValue(objAPSupplierContactsInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }
                }
            }
            //else, copy main contact and address of supplier to PO Contact
            else
            {
                APSuppliersController objAPSuppliersController = new APSuppliersController();
                APSuppliersInfo objAPSuppliersInfo = (APSuppliersInfo)objAPSuppliersController.GetObjectByID(iAPSupplierID);
                if (objAPSuppliersInfo != null)
                {
                    String strSupplierTableName = THSUtil.GetTableNameFromBusinessObject(objAPSuppliersInfo);

                    PropertyInfo[] properties = MainObject.GetType().GetProperties();

                    foreach (PropertyInfo prop in properties)
                    {
                        if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "DeliveryContact"))
                        {
                            String strSupplierPropertyName = strSupplierTableName.Substring(0, strSupplierTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 7);
                            PropertyInfo supplierProperty = objAPSuppliersInfo.GetType().GetProperty(strSupplierPropertyName);
                            if (supplierProperty != null)
                            {
                                object objValue = supplierProperty.GetValue(objAPSuppliersInfo, null);
                                prop.SetValue(MainObject, objValue, null);
                            }
                        }

                        if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "DeliveryAddress"))
                        {
                            String strSupplierPropertyName = strSupplierTableName.Substring(0, strSupplierTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 7);
                            PropertyInfo supplierProperty = objAPSuppliersInfo.GetType().GetProperty(strSupplierPropertyName);
                            if (supplierProperty != null)
                            {
                                object objValue = supplierProperty.GetValue(objAPSuppliersInfo, null);
                                prop.SetValue(MainObject, objValue, null);
                            }
                        }
                    }
                }
            }
        }

        public void SetInvoiceContactFromSupplier(int iAPSupplierID)
        {

            String strMainObjectTableName = THSUtil.GetTableNameFromBusinessObject(MainObject);

            APSupplierContactsController objAPSupplierContactsController = new APSupplierContactsController();
            List<APSupplierContactsInfo> lstAPSupplierContactsInfo = objAPSupplierContactsController.GetAPSupplierContactsListByAPSupplierIDAndAPSupplierContactType(iAPSupplierID, ADConfigValueUtility.cstContactTypeInvoice);

            //If exist Supplier Contact with type is Purchase Order, copy contact and address of first Supplier Contact in list to PO Contact
            if (lstAPSupplierContactsInfo.Count > 0)
            {
                APSupplierContactsInfo objAPSupplierContactsInfo = lstAPSupplierContactsInfo[0];
                String strSupplierContactTableName = THSUtil.GetTableNameFromBusinessObject(objAPSupplierContactsInfo);

                PropertyInfo[] properties = MainObject.GetType().GetProperties();

                foreach (PropertyInfo prop in properties)
                {
                    if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "InvoiceContact"))
                    {
                        String strSupplierContactPropertyName = strSupplierContactTableName.Substring(0, strSupplierContactTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 13);
                        PropertyInfo supplierContactProperty = objAPSupplierContactsInfo.GetType().GetProperty(strSupplierContactPropertyName);
                        if (supplierContactProperty != null)
                        {
                            object objValue = supplierContactProperty.GetValue(objAPSupplierContactsInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }

                    if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "POAddress"))
                    {
                        String strSupplierContactPropertyName = strSupplierContactTableName.Substring(0, strSupplierContactTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 6);
                        PropertyInfo supplierContactProperty = objAPSupplierContactsInfo.GetType().GetProperty(strSupplierContactPropertyName);
                        if (supplierContactProperty != null)
                        {
                            object objValue = supplierContactProperty.GetValue(objAPSupplierContactsInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }
                }
            }
            //else, copy main contact and address of supplier to PO Contact
            else
            {
                APSuppliersController objAPSuppliersController = new APSuppliersController();
                APSuppliersInfo objAPSuppliersInfo = (APSuppliersInfo)objAPSuppliersController.GetObjectByID(iAPSupplierID);
                if (objAPSuppliersInfo != null)
                {
                    String strSupplierTableName = THSUtil.GetTableNameFromBusinessObject(objAPSuppliersInfo);

                    PropertyInfo[] properties = MainObject.GetType().GetProperties();

                    foreach (PropertyInfo prop in properties)
                    {
                        if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "InvoiceContact"))
                        {
                            String strSupplierPropertyName = strSupplierTableName.Substring(0, strSupplierTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 6);
                            PropertyInfo supplierProperty = objAPSuppliersInfo.GetType().GetProperty(strSupplierPropertyName);
                            if (supplierProperty != null)
                            {
                                object objValue = supplierProperty.GetValue(objAPSuppliersInfo, null);
                                prop.SetValue(MainObject, objValue, null);
                            }
                        }

                        if (prop.Name.StartsWith(strMainObjectTableName.Substring(0, strMainObjectTableName.Length - 1) + "InvoiceAddress"))
                        {
                            String strSupplierPropertyName = strSupplierTableName.Substring(0, strSupplierTableName.Length - 1) + prop.Name.Substring(strMainObjectTableName.Length + 6);
                            PropertyInfo supplierProperty = objAPSuppliersInfo.GetType().GetProperty(strSupplierPropertyName);
                            if (supplierProperty != null)
                            {
                                object objValue = supplierProperty.GetValue(objAPSuppliersInfo, null);
                                prop.SetValue(MainObject, objValue, null);
                            }
                        }
                    }
                }
            }
        }
        #endregion

        public virtual void SetDefaultValuesFromSupplier()
        {
           
            String strMainObjectName = THSUtil.GetTableNameFromBusinessObject(MainObject);
            String strMainObjectPaymentTermPropertyName = strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "PaymentTerm";
            String strMainObjectTableNamePrefix = strMainObjectName.Substring(0, 2);

            int iAPSupplierID = Convert.ToInt32(THSDbUtil.GetPropertyValue(MainObject, "FK_APSupplierID"));

            if (iAPSupplierID > 0)
            {
                APSuppliersInfo objAPSuppliersInfo = (APSuppliersInfo)new APSuppliersController().GetObjectByID(iAPSupplierID);
                if (objAPSuppliersInfo != null)
                {

                    SetPOContactFromSupplier(iAPSupplierID);
                    SetDeliveryContactFromSupplier(iAPSupplierID);
                    SetInvoiceContactFromSupplier(iAPSupplierID);


                    //THSDbUtil.SetPropertyValue(MainObject, String.Format("{0}BuyPriceCombo", strMainObjectTableNamePrefix), objAPSuppliersInfo.APBuyPriceCombo);

                    THSDbUtil.SetPropertyValue(MainObject, String.Format("{0}PaymentMethodCombo", strMainObjectTableNamePrefix), objAPSuppliersInfo.APPaymentMethodCombo);

                    THSDbUtil.SetPropertyValue(MainObject, strMainObjectPaymentTermPropertyName, objAPSuppliersInfo.APSupplierPaymentTerm);

                    //THSDbUtil.SetPropertyValue(MainObject, "FK_APSellerID", objAPSuppliersInfo.FK_APSellerIDCombo);

                    THSDbUtil.SetPropertyValue(MainObject, "FK_GELanguageID", objAPSuppliersInfo.FK_GELanguageID);

                    THSDbUtil.SetPropertyValue(MainObject, "FK_GECurrencyID", objAPSuppliersInfo.FK_GECurrencyIDCombo);
                }
                UpdateMainObjectBindingSource();
            }
        }
        #endregion

        
        public virtual void SetDefaultValuesFromEmployee(int iEmployeeID, String strPrimaryTableName)
        {
            HREmployeesController objEmployeesController = new HREmployeesController();
            HREmployeesInfo objEmployeesInfo = (HREmployeesInfo)objEmployeesController.GetObjectByID(iEmployeeID);
            if (objEmployeesInfo != null)
            {
                String strMainTableName = THSUtil.GetTableNameFromBusinessObject(MainObject);
                String strForeignTableName = THSUtil.GetTableNameFromBusinessObjectType(typeof(HREmployeesInfo));
                String strForeignTableNamePrefix = strForeignTableName.Substring(0, 2);

               
                PropertyInfo[] properties = new PropertyInfo[0];
                if (strPrimaryTableName == strMainTableName)
                    properties = MainObject.GetType().GetProperties();
                else
                    properties = ModuleObjects[strPrimaryTableName].GetType().GetProperties();
                foreach (PropertyInfo prop in properties)
                    if (!THSDbUtil.IsForeignKey(strPrimaryTableName, prop.Name))
                    {
                        int iIndex = prop.Name.IndexOf(strForeignTableName.Substring(2, strForeignTableName.Length - 3));
                        if (iIndex >= 0)
                        {
                            String strForeignPropertyName = strForeignTableNamePrefix + prop.Name.Substring(iIndex, prop.Name.Length - iIndex);
                            object objForeignPropertyValue = THSDbUtil.GetPropertyValue(objEmployeesInfo, strForeignPropertyName);
                            if (objForeignPropertyValue != null)
                            {
                                if (strPrimaryTableName == strMainTableName)
                                    THSDbUtil.SetPropertyValue(MainObject, prop.Name, objForeignPropertyValue);
                                else
                                    THSDbUtil.SetPropertyValue(ModuleObjects[strPrimaryTableName], prop.Name, objForeignPropertyValue);
                            }
                        }
                    }


                if (strPrimaryTableName == strMainTableName)
                    UpdateMainObjectBindingSource();
                else
                    UpdateModuleObjectBindingSource(strPrimaryTableName);
            }
        }
#if never
        #region Set Default Values From Supplier
        public virtual void SetCommandContactFromSupplier(int iMASupplierID)
        {
            String strMainObjectName = THSUtil.GetTableNameFromBusinessObject(MainObject);

            MAContactsController objMAContactsController = new MAContactsController();
            DataSet dsMAContacts = objMAContactsController.GetMAContactsByMASupplierIDAndMAContactType(iMASupplierID, ADConfigValueUtility.ContactTypeSellOrder);

            if (dsMAContacts.Tables[0].Rows.Count > 0)
            {
                MAContactsInfo objMAContactsInfo = (MAContactsInfo)objMAContactsController.GetObjectFromDataRow(dsMAContacts.Tables[0].Rows[0]);
                MAAddressInfo objMAAddressInfo = new MAAddressInfo();
                if (objMAContactsInfo.MAAddressID > 0)
                {
                    objMAAddressInfo = (MAAddressInfo)new MAAddressController().GetObjectByID(objMAContactsInfo.MAAddressID);
                }

                PropertyInfo[] properties = MainObject.GetType().GetProperties();

                foreach (PropertyInfo prop in properties)
                {
                    if (prop.Name.StartsWith(strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "CommandContact"))
                    {
                        String strContactPropertyName = "MA" + prop.Name.Substring(strMainObjectName.Length + 6);
                        PropertyInfo contactProperty = objMAContactsInfo.GetType().GetProperty(strContactPropertyName);
                        if (contactProperty != null)
                        {
                            object objValue = contactProperty.GetValue(objMAContactsInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }

                    if (prop.Name.StartsWith(strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "CommandAddress"))
                    {
                        if (objMAContactsInfo.MAAddressID > 0)
                        {
                            String strAddressPropertyName = "MA" + prop.Name.Substring(strMainObjectName.Length + 6);
                            PropertyInfo addressProperty = objMAAddressInfo.GetType().GetProperty(strAddressPropertyName);
                            if (addressProperty != null)
                            {
                                object objValue = addressProperty.GetValue(objMAAddressInfo, null);
                                prop.SetValue(MainObject, objValue, null);
                            }
                        }
                    }
                }

            }
            else
            {
                MASuppliersInfo objMASuppliersInfo = (MASuppliersInfo)new MASuppliersController().GetObjectByID(iMASupplierID);
                PropertyInfo[] properties = MainObject.GetType().GetProperties();

                foreach (PropertyInfo prop in properties)
                {
                    if (prop.Name.StartsWith(strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "CommandContact"))
                    {
                        String strSupplierPropertyName = "MASupplier" + prop.Name.Substring(strMainObjectName.Length + 6);
                        PropertyInfo SupplierProperty = objMASuppliersInfo.GetType().GetProperty(strSupplierPropertyName);
                        if (SupplierProperty != null)
                        {
                            object objValue = SupplierProperty.GetValue(objMASuppliersInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }

                    if (prop.Name.StartsWith(strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "CommandAddress"))
                    {
                        String strSupplierPropertyName = "MASupplier" + prop.Name.Substring(strMainObjectName.Length + 6);
                        PropertyInfo SupplierProperty = objMASuppliersInfo.GetType().GetProperty(strSupplierPropertyName);
                        if (SupplierProperty != null)
                        {
                            object objValue = SupplierProperty.GetValue(objMASuppliersInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }
                }
            }


        }

        public virtual void SetInvoiceContactFromSupplier(int iMASupplierID)
        {
            String strMainObjectName = THSUtil.GetTableNameFromBusinessObject(MainObject);

            MAContactsController objMAContactsController = new MAContactsController();
            DataSet dsMAContacts = objMAContactsController.GetMAContactsByMASupplierIDAndMAContactType(iMASupplierID, ADConfigValueUtility.ContactTypeInvoice);

            if (dsMAContacts.Tables[0].Rows.Count > 0)
            {
                MAContactsInfo objMAContactsInfo = (MAContactsInfo)objMAContactsController.GetObjectFromDataRow(dsMAContacts.Tables[0].Rows[0]);
                MAAddressInfo objMAAddressInfo = new MAAddressInfo();
                if (objMAContactsInfo.MAAddressID > 0)
                {
                    objMAAddressInfo = (MAAddressInfo)new MAAddressController().GetObjectByID(objMAContactsInfo.MAAddressID);
                }

                PropertyInfo[] properties = MainObject.GetType().GetProperties();

                foreach (PropertyInfo prop in properties)
                {
                    if (prop.Name.StartsWith(strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "InvoiceContact"))
                    {
                        String strContactPropertyName = "MA" + prop.Name.Substring(strMainObjectName.Length + 6);
                        PropertyInfo contactProperty = objMAContactsInfo.GetType().GetProperty(strContactPropertyName);
                        if (contactProperty != null)
                        {
                            object objValue = contactProperty.GetValue(objMAContactsInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }

                    if (prop.Name.StartsWith(strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "InvoiceAddress"))
                    {
                        if (objMAContactsInfo.MAAddressID > 0)
                        {
                            String strAddressPropertyName = "MA" + prop.Name.Substring(strMainObjectName.Length + 6);
                            PropertyInfo addressProperty = objMAAddressInfo.GetType().GetProperty(strAddressPropertyName);
                            if (addressProperty != null)
                            {
                                object objValue = addressProperty.GetValue(objMAAddressInfo, null);
                                prop.SetValue(MainObject, objValue, null);
                            }
                        }
                    }
                }

            }
            else
            {
                MASuppliersInfo objMASuppliersInfo = (MASuppliersInfo)new MASuppliersController().GetObjectByID(iMASupplierID);
                PropertyInfo[] properties = MainObject.GetType().GetProperties();

                foreach (PropertyInfo prop in properties)
                {
                    if (prop.Name.StartsWith(strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "InvoiceContact"))
                    {
                        String strSupplierPropertyName = "MASupplier" + prop.Name.Substring(strMainObjectName.Length + 6);
                        PropertyInfo SupplierProperty = objMASuppliersInfo.GetType().GetProperty(strSupplierPropertyName);
                        if (SupplierProperty != null)
                        {
                            object objValue = SupplierProperty.GetValue(objMASuppliersInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }

                    if (prop.Name.StartsWith(strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "InvoiceAddress"))
                    {
                        String strSupplierPropertyName = "MASupplier" + prop.Name.Substring(strMainObjectName.Length + 6);
                        PropertyInfo SupplierProperty = objMASuppliersInfo.GetType().GetProperty(strSupplierPropertyName);
                        if (SupplierProperty != null)
                        {
                            object objValue = SupplierProperty.GetValue(objMASuppliersInfo, null);
                            prop.SetValue(MainObject, objValue, null);
                        }
                    }
                }
            }
        }

        public virtual void SetDefaultValuesFromSupplier()
        {
           
            String strMainObjectName = THSUtil.GetTableNameFromBusinessObject(MainObject);
            String strMainObjectSkontoPropertyName = strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "Skonto";


            int iMASupplierID = Convert.ToInt32(THSDbUtil.GetPropertyValue(MainObject, "FASupplierIDCombo"));

            if (iMASupplierID > 0)
            {
                MASuppliersInfo objMASuppliersInfo = (MASuppliersInfo)new MASuppliersController().GetObjectByID(iMASupplierID);
                if (objMASuppliersInfo != null)
                {
                    SetCommandContactFromSupplier(iMASupplierID);
                    SetInvoiceContactFromSupplier(iMASupplierID);

                    THSDbUtil.SetPropertyValue(MainObject, strMainObjectSkontoPropertyName, objMASuppliersInfo.MASupplierSkonto);
                    THSDbUtil.SetPropertyValue(MainObject, "FALanguageIDCombo", objMASuppliersInfo.MALanguageIDCombo);
                    THSDbUtil.SetPropertyValue(MainObject, "FACurrencyIDCombo", objMASuppliersInfo.MACurrencyIDCombo);
                }
                UpdateMainObjectBindingSource();

            }
        }
        #endregion
#endif        

        #region Set Default Values From Product
        public virtual void SetValuesAfterValidateProduct(int iICProductID)
        {
            
        }

        public virtual void SetValuesAfterValidateProductQty()
        {
            SetProductPrice();
            SetBasicAndPackageQty();
            //SetProductPriceByProductUnitPrice();
        }

        public virtual void SetValuesAfterValidateProductUnitPrice()
        {
            SetProductPriceByProductUnitPrice();
        }
      

        public virtual double SetDefaultProductTaxPercent(ICProductsInfo objICProductsInfo)
        {
            double dbTaxPercent = 0;
            int iGEVATID = objICProductsInfo.FK_GEVATID;
            if (iGEVATID > 0)
            {
                GEVATsController objGEVATsController = new GEVATsController();
                GEVATsInfo objGEVATsInfo = (GEVATsInfo)objGEVATsController.GetObjectByID(iGEVATID);
                if (objGEVATsInfo != null)
                {
                    dbTaxPercent = objGEVATsInfo.GEVATPercentValue;

                }
            }
            return dbTaxPercent;

        }

        public virtual void SetProductPrice(BusinessObject objItem)
        {
           

            String strMainTableName = THSUtil.GetTableNameFromBusinessObject(MainObject);
            String strItemTableName = THSUtil.GetTableNameFromBusinessObject(objItem);

            int iICProductID = Convert.ToInt32(THSDbUtil.GetPropertyValue(objItem, "FK_ICProductID"));

            String strColumnName = strMainTableName.Substring(0, strMainTableName.Length - 1) + "Date";
            DateTime dtMainObjectStartDate = (DateTime)THSDbUtil.GetPropertyValue(MainObject, strColumnName);

            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductQty";
            double dbItemProductQty = Convert.ToDouble(THSDbUtil.GetPropertyValue(objItem, strColumnName));
            if (dbItemProductQty == 0)
            {
                dbItemProductQty = 1.0;
                THSDbUtil.SetPropertyValue(objItem, strColumnName, 1.0);
            }

            int iGECurrency = 0;
            object propertyValue = THSDbUtil.GetPropertyValue(MainObject, "FK_GECurrencyID");
            if (propertyValue != null)
                iGECurrency = (int)THSDbUtil.GetPropertyValue(MainObject, "FK_GECurrencyID");

            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductTaxPercent";
            double dbItemProductTaxPercent = Convert.ToDouble(THSDbUtil.GetPropertyValue(objItem, strColumnName));

            double dbItemProductUnitPrice = CalculateProductPrice(iICProductID, dtMainObjectStartDate, dbItemProductQty, iGECurrency);

            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductUnitCost";
            double dbItemProductUnitCost = Convert.ToDouble(THSDbUtil.GetPropertyValue(objItem, strColumnName));

            //Set Product Unit Price
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductUnitPrice";
            THSDbUtil.SetPropertyValue(objItem, strColumnName, dbItemProductUnitPrice);

            //Set Item Price
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "Price";
            double dbItemPrice = dbItemProductQty * dbItemProductUnitPrice;
            THSDbUtil.SetPropertyValue(objItem, strColumnName, dbItemPrice);

            //Set Item Net Amount
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "NetAmount";
            double dbItemNetAmount = dbItemPrice;
            THSDbUtil.SetPropertyValue(objItem, strColumnName, dbItemNetAmount);

            //Set Item Discount Amount                
            double dbItemDiscountAmount = CalculateItemDiscountAmount(iICProductID, dtMainObjectStartDate, dbItemProductQty);
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "DiscountAmount";
            THSDbUtil.SetPropertyValue(objItem, strColumnName, dbItemDiscountAmount);
            
            //Set Item Tax Amount
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "TaxAmount";
            double dbItemTaxAmount = (dbItemPrice - dbItemDiscountAmount) * dbItemProductTaxPercent / 100;
            THSDbUtil.SetPropertyValue(objItem, strColumnName, dbItemTaxAmount);

            //Set Item Total Amount
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "TotalAmount";
            THSDbUtil.SetPropertyValue(objItem, strColumnName, dbItemNetAmount + dbItemTaxAmount - dbItemDiscountAmount);

            //Set Item Total Cost
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "TotalCost";
            THSDbUtil.SetPropertyValue(objItem, strColumnName, dbItemProductQty * dbItemProductUnitCost);

        }

        public virtual void SetProductPrice(ERPModuleItemsEntity entItem, String strItemTableName)
        {
            BusinessObject obj = entItem.SetToBusinessObject(strItemTableName);
            SetProductPrice(obj);
            entItem.GetFromBusinessObject(obj);
        }

        public virtual void SetProductPrice()
        {
            String strMainTableName = THSUtil.GetTableNameFromBusinessObject(MainObject);
            String strItemTableName = strMainTableName.Substring(0, strMainTableName.Length - 1) + "Items";
            if (ModuleObjects[strItemTableName] != null)
            {
                SetProductPrice(ModuleObjects[strItemTableName]);
                UpdateModuleObjectBindingSource(strItemTableName);
            }
        }

        public virtual double CalculateItemDiscountAmount(int productID, DateTime transactionDate, double itemQty)
        {
           
            double discountAmount = 0;
            ICProductsController objProductsController = new ICProductsController();
            ICProductsInfo objProductsInfo = (ICProductsInfo)objProductsController.GetObjectByID(productID);
            if (objProductsInfo != null)
            {
                double productPrice = CalculateProductPriceBaseOnProductPriceLevel(objProductsInfo);
                
                String mainTableName = THSUtil.GetTableNameFromBusinessObject(MainObject);

                //Calculate discount from customer
                bool check = Convert.ToBoolean(THSDbUtil.GetPropertyValue(MainObject, mainTableName.Substring(0, mainTableName.Length - 1) + "CustomerDiscountCheck"));
                if (check)
                {
                    int customerID = Convert.ToInt32(THSDbUtil.GetPropertyValue(MainObject, "FK_ARCustomerID"));
                    if (customerID > 0)
                    {
                        ARCustomersController objCustomersController = new ARCustomersController();
                        ARCustomersInfo objCustomersInfo = (ARCustomersInfo)objCustomersController.GetObjectByID(customerID);
                        if (objCustomersInfo != null)
                            discountAmount += (productPrice * objCustomersInfo.ARCustomerDiscount / 100) * itemQty;
                    }
                }

                //Calculate discount from promotion
                check = Convert.ToBoolean(THSDbUtil.GetPropertyValue(MainObject, mainTableName.Substring(0, mainTableName.Length - 1) + "PromotionDiscountCheck"));
                if (check)
                {
                    ICPromotionsController objPromotionsController = new ICPromotionsController();
                    ICPromotionItemsController objPromotionItemsController = new ICPromotionItemsController();
                    ICPromotionItemsInfo objPromotionItemsInfo = (ICPromotionItemsInfo)objPromotionItemsController.GetFirstObjectByForeignColumn("FK_ICProductID", productID);
                    if (objPromotionItemsInfo != null)
                    {
                        ICPromotionsInfo objPromotionsInfo = (ICPromotionsInfo)objPromotionsController.GetObjectByID(objPromotionItemsInfo.FK_ICPromotionID);
                        if (transactionDate.CompareTo(objPromotionsInfo.ICPromotionStartDate) >= 0 && transactionDate.CompareTo(objPromotionsInfo.ICPromotionEndDate) <= 0)
                        {
                            if (objPromotionsInfo.ICPromotionDiscountPercent > 0)
                                discountAmount += Math.Floor(itemQty / objPromotionsInfo.ICPromotionDiscountQty) * (objPromotionsInfo.ICPromotionDiscountQty * productPrice * (objPromotionsInfo.ICPromotionDiscountPercent / 100));
                            else if (objPromotionsInfo.ICPromotionDiscountAmount > 0)
                                discountAmount += Math.Floor(itemQty / objPromotionsInfo.ICPromotionDiscountQty) * objPromotionsInfo.ICPromotionDiscountAmount;
                        }
                    }
                }
            }
            return discountAmount;
        }

        public void SetBasicAndPackageQty()
        {
            try
            {
               

                String strMainTableName = THSUtil.GetTableNameFromBusinessObject(MainObject);
                String strItemTableName = strMainTableName.Substring(0, strMainTableName.Length - 1) + "Items";

                String strItemProductQtyColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductQty";
                String strItemProductSellFactorColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductSellFactor";
                String strItemProductPackagingFactorColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductPackagingFactor";
                String strItemProductBasicQtyColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductBasicQty";
                String strItemProductPackagingQtyColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductPakagingQty";

                int iICProductID = Convert.ToInt32(THSDbUtil.GetPropertyValue(ModuleObjects[strItemTableName], "FK_ICProductID"));

                double dbItemSellQty = Convert.ToDouble(THSDbUtil.GetPropertyValue(ModuleObjects[strItemTableName], strItemProductQtyColumnName));
                double dbSellFactor = Convert.ToDouble(THSDbUtil.GetPropertyValue(ModuleObjects[strItemTableName], strItemProductSellFactorColumnName));
                double dbItemPackagingFactor = Convert.ToDouble(THSDbUtil.GetPropertyValue(ModuleObjects[strItemTableName], strItemProductPackagingFactorColumnName));

                if (dbSellFactor > 0)
                {
                    double dbItemBasicQty = dbItemSellQty / dbSellFactor;
                    double dbItemPackagingQty = 0;
                    if (dbItemPackagingFactor > 0)
                        dbItemPackagingQty = dbItemBasicQty / dbItemPackagingFactor;

                    THSDbUtil.SetPropertyValue(ModuleObjects[strItemTableName], strItemProductBasicQtyColumnName, dbItemBasicQty);
                    THSDbUtil.SetPropertyValue(ModuleObjects[strItemTableName], strItemProductPackagingQtyColumnName, dbItemPackagingQty);
                }
                else
                {
                    THSDbUtil.SetPropertyValue(ModuleObjects[strItemTableName], strItemProductBasicQtyColumnName, 0);
                    THSDbUtil.SetPropertyValue(ModuleObjects[strItemTableName], strItemProductPackagingQtyColumnName, 0);
                }

            }
            catch (Exception)
            {
                MessageBox.Show("SetBasicAndPackageQty ", "THS Bug");
                return;
            }
        }

        public virtual void SetProductPriceByProductUnitPrice(BusinessObject objItem)
        {
           

            String strMainTableName = THSUtil.GetTableNameFromBusinessObject(MainObject);
            String strItemTableName = THSUtil.GetTableNameFromBusinessObject(objItem);

            int iICProductID = Convert.ToInt32(THSDbUtil.GetPropertyValue(objItem, "FK_ICProductID"));

            String strColumnName = strMainTableName.Substring(0, strMainTableName.Length - 1) + "Date";
            DateTime dtMainObjectStartDate = (DateTime)THSDbUtil.GetPropertyValue(MainObject, strColumnName);

            String strItemProductUnitPriceColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductUnitPrice";
            String strItemProductQtyColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductQty";
            String strItemPriceColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "Price";

            double dbProductUnitPrice = Convert.ToDouble(THSDbUtil.GetPropertyValue(objItem, strItemProductUnitPriceColumnName));
            double dbProductQty = Convert.ToDouble(THSDbUtil.GetPropertyValue(objItem, strItemProductQtyColumnName));
            double dbItemPrice = dbProductUnitPrice * dbProductQty;
            THSDbUtil.SetPropertyValue(objItem, strItemPriceColumnName, dbItemPrice);

            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductTaxPercent";
            double dbItemProductTaxPercent = Convert.ToDouble(THSDbUtil.GetPropertyValue(objItem, strColumnName));

            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductUnitCost";
            double dbProductUnitCost = Convert.ToDouble(THSDbUtil.GetPropertyValue(objItem, strColumnName));

            //Set Item Net Amount
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "NetAmount";
            double dbItemNetAmount = dbItemPrice;
            THSDbUtil.SetPropertyValue(objItem, strColumnName, dbItemNetAmount);

            //Set Item Discount Amount                
            double dbItemDiscountAmount = CalculateItemDiscountAmount(iICProductID, dtMainObjectStartDate, dbProductQty);
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "DiscountAmount";
            THSDbUtil.SetPropertyValue(objItem, strColumnName, dbItemDiscountAmount);

            //Set Item Tax Amount
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "TaxAmount";
            double dbItemTaxAmount = (dbItemPrice - dbItemDiscountAmount) * dbItemProductTaxPercent / 100;
            THSDbUtil.SetPropertyValue(objItem, strColumnName, dbItemTaxAmount);

            //Set Item Total Amount
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "TotalAmount";
            THSDbUtil.SetPropertyValue(objItem, strColumnName, dbItemNetAmount + dbItemTaxAmount - dbItemDiscountAmount);

            //Set Item Total Cost
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "TotalCost";
            THSDbUtil.SetPropertyValue(objItem, strColumnName, dbProductQty * dbProductUnitCost);

        }

        public virtual void SetProductPriceByProductUnitPrice(ERPModuleItemsEntity entItem, String strItemTableName)
        {
            BusinessObject obj = entItem.SetToBusinessObject(strItemTableName);
            SetProductPriceByProductUnitPrice(obj);
            entItem.GetFromBusinessObject(obj);
        }

        public virtual void SetProductPriceByProductUnitPrice()
        {
            String strMainTableName = THSUtil.GetTableNameFromBusinessObject(MainObject);
            String strItemTableName = strMainTableName.Substring(0, strMainTableName.Length - 1) + "Items";
            if (ModuleObjects[strItemTableName] != null)
            {
                SetProductPriceByProductUnitPrice(ModuleObjects[strItemTableName]);
                UpdateModuleObjectBindingSource(strItemTableName);
            }
        }
        #region Calculate Product Price

        public virtual double CalculateProductPrice(int iICProductID, DateTime dtTransactionDate,
                                       double dbProductQty, int iGECurrencyID)
        {
            double dbProductPrice = 0;
            //Get MAProductsInfo object
            ICProductsController objICProductsController = new ICProductsController();
            ICProductsInfo objICProductsInfo = (ICProductsInfo)objICProductsController.GetObjectByID(iICProductID);
            if (objICProductsInfo != null)
            {
                if (!CalcuateProductPriceBaseOnPriceList(objICProductsInfo.ICProductID, dtTransactionDate, dbProductQty, iGECurrencyID, ref dbProductPrice))
                {
                    //Default,get Price from Product Price Level
                    dbProductPrice = CalculateProductPriceBaseOnProductPriceLevel(objICProductsInfo);
                }
            }

            return dbProductPrice;
        }

        protected virtual double CalculateProductPriceBaseOnProductPriceLevel(ICProductsInfo objICProductsInfo)
        {
           
            double dbProductPrice = objICProductsInfo.ICProductPrice01;
            int priceLevelID = Convert.ToInt32(THSDbUtil.GetPropertyValue(MainObject, "FK_ARPriceLevelID"));
            if (priceLevelID > 0)
            {
                ICProductPricesController objProductPricesController = new ICProductPricesController();
                ICProductPricesInfo objProductPricesInfo = objProductPricesController.GetProductPriceByProductIDAndPriceLevelID(objICProductsInfo.ICProductID, priceLevelID);
                //If product has own price level
                if (objProductPricesInfo != null)
                    dbProductPrice = (1 - objProductPricesInfo.ICProductPriceMarkDown / 100) * objICProductsInfo.ICProductPrice01;
                //Else, get default price level
                else
                {
                    ARPriceLevelsController objPriceLevelsController = new ARPriceLevelsController();
                    ARPriceLevelsInfo objPriceLevelsInfo = (ARPriceLevelsInfo)objPriceLevelsController.GetObjectByID(priceLevelID);
                    if (objPriceLevelsInfo != null)
                        dbProductPrice = (1 - objPriceLevelsInfo.ARPriceLevelMarkDown / 100) * objICProductsInfo.ICProductPrice01;
                }
            }
            return dbProductPrice;
        }

        protected virtual bool CalcuateProductPriceBaseOnPriceList(int iICProductID, DateTime dtTransactionDate,
                                                      double dbProductQty, int iGECurrencyID, ref double dbProductPrice)
        {
            //double dbProductPrice = 0;
           
            bool bFlag = false;
            int iARCustomerID = Convert.ToInt32(THSDbUtil.GetPropertyValue(MainObject, "FK_ARCustomerID"));
            ARCustomersController objARCustomersController = new ARCustomersController();
            ARCustomersInfo objARCustomersInfo = (ARCustomersInfo)objARCustomersController.GetObjectByID(iARCustomerID);

            if (objARCustomersInfo != null)
            {
                ARPriceListDetailsController objARPriceListDetailsController = new ARPriceListDetailsController();
                if (objARCustomersInfo.FK_ARPriceListID2 > 0)
                {
                    DataSet dsPriceList2Details = objARPriceListDetailsController.GetPriceListDetailsByPriceListAndPriceListDateAndProductAndProductQtyAndCurrency(
                                                                                objARCustomersInfo.FK_ARPriceListID2, dtTransactionDate, iICProductID,
                                                                                dbProductQty, iGECurrencyID);
                    if (dsPriceList2Details.Tables.Count > 0)
                    {
                        if (dsPriceList2Details.Tables[0].Rows.Count > 0)
                        {
                            bFlag = true;
                            DataRow rowPriceList2Detail = dsPriceList2Details.Tables[0].Rows[0];
                            ARPriceListDetailsInfo objARPriceListDetailsInfo = (ARPriceListDetailsInfo)objARPriceListDetailsController.GetObjectFromDataRow(rowPriceList2Detail);
                            if (objARPriceListDetailsInfo != null)
                                dbProductPrice = objARPriceListDetailsInfo.ARPriceListDetailProductSalePrice;
                        }
                        else
                        {
                            if (objARCustomersInfo.FK_ARPriceListID1 > 0)
                            {
                                DataSet dsPriceList1Details = objARPriceListDetailsController.GetPriceListDetailsByPriceListAndPriceListDateAndProductAndProductQtyAndCurrency(
                                                                                            objARCustomersInfo.FK_ARPriceListID1, dtTransactionDate, iICProductID,
                                                                                            dbProductQty, iGECurrencyID);
                                if (dsPriceList1Details.Tables.Count > 0)
                                {
                                    if (dsPriceList1Details.Tables[0].Rows.Count > 0)
                                    {
                                        bFlag = true;
                                        DataRow rowPriceList1Detail = dsPriceList1Details.Tables[0].Rows[0];
                                        ARPriceListDetailsInfo objARPriceListDetailsInfo = (ARPriceListDetailsInfo)objARPriceListDetailsController.GetObjectFromDataRow(rowPriceList1Detail);
                                        if (objARPriceListDetailsInfo != null)
                                            dbProductPrice = objARPriceListDetailsInfo.ARPriceListDetailProductSalePrice;
                                    }
                                }
                            }
                        }
                    }
                }

            }


            return bFlag;
        }
       
        #endregion

        #endregion

        #region Get Range Days and Discount Percent from Payment Term
        public int GetRangeDays1FromPaymentTerm(String strPaymentTerm)
        {
            String strRangeDays1 = strPaymentTerm.Substring(0, 3);
            return Convert.ToInt32(strRangeDays1);
        }

        public double GetPercentDiscount1FromPaymentTerm(String strPaymentTerm)
        {
            int index = strPaymentTerm.IndexOf("%");
            String strPercentDiscount = strPaymentTerm.Substring(index - 4, 3);
            return Convert.ToDouble(strPercentDiscount);
        }

        public int GetRangeDays2FromPaymentTerm(String strPaymentTerm)
        {
            int index = strPaymentTerm.IndexOf(";");
            String strRangeDays2 = strPaymentTerm.Substring(index + 2, 3);
            return Convert.ToInt32(strRangeDays2);
        }

        public double GetPercentDiscount2FromPaymentTerm(String strPaymentTerm)
        {
            int index = strPaymentTerm.LastIndexOf("%");

            String strPercentDiscount = strPaymentTerm.Substring(index - 4, 3);
            return Convert.ToDouble(strPercentDiscount);
        }

        public int GetRangeDays3FromPaymentTerm(String strPaymentTerm)
        {
            int index = strPaymentTerm.LastIndexOf(";");
            String strRangeDays3 = strPaymentTerm.Substring(index + 2, 3);
            return Convert.ToInt32(strRangeDays3);
        }
        #endregion       

        #region Calculate Total Information
        public virtual void CalculateTotalInformation(ref double dbTotalPrice,
                                                      ref double dbTotalItemDiscountAmount,
                                                      ref double dbDiscountAmount,
                                                      ref double dbTotalTaxAmount,
                                                      ref double dbTotalBuyingPrice,
                                                      ref double dbTotalItemNetAmount,
                                                      ref double dbTotalNetAmount,
                                                      ref double dbTotalGrossAmount,
                                                      ref double dbMarginPercent)
        {

        }
        #endregion

        /// <summary>
        /// Check before update inventory
        /// </summary>
        ///
        public virtual bool IsInvalidInventory()
        {
            return false;
        }

        public virtual bool IsInvalidInventory(BusinessObject obj, String transactionType)
        {
            String tableName = THSUtil.GetTableNameFromBusinessObject(obj);
            TransactionUtil.InventoryStatus status = GetInventoryStatus(obj, tableName, transactionType);
            if (status == TransactionUtil.InventoryStatus.Empty)
            {
                MessageBox.Show("Nguyên vật liệu chế biến món này đã hết.", "#Cảnh báo#", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            return false;
        }

        /// <summary>
        /// Get inventory status: empty, less than minimum quantity or greater than maximum quantity
        /// </summary>
        /// <remarks></remarks>
        /// 
        public virtual TransactionUtil.InventoryStatus GetInventoryStatus(BusinessObject obj, String itemTableName, String transactionType)
        {
           
            int productID = Convert.ToInt32(THSDbUtil.GetPropertyValue(obj, "FK_ICProductID"));
            //int productAttributeID = Convert.ToInt32(THSDbUtil.GetPropertyValue(obj, "FK_ICProductAttributeID"));
            int stockID = Convert.ToInt32(THSDbUtil.GetPropertyValue(obj, "FK_ICStockID"));
            double itemQty = Convert.ToInt32(THSDbUtil.GetPropertyValue(obj, itemTableName.Substring(0, itemTableName.Length - 1) + "ProductQty"));

            ICInventoryStocksController objInventoryStocksController = new ICInventoryStocksController();
            //ICInventoryStocksInfo objInventoryStocksInfo = objInventoryStocksController.GetInventoryStockByStockIDAndProductIDAndProductAttributeID(stockID, productID, productAttributeID);
            ICInventoryStocksInfo objInventoryStocksInfo = objInventoryStocksController.GetInventoryStockByProductAndStock(productID,stockID);
            if (objInventoryStocksInfo == null)
                objInventoryStocksInfo = new ICInventoryStocksInfo();
            switch (transactionType)
            {
                case TransactionUtil.cstInventoryReceipt:
                    objInventoryStocksInfo.ICInventoryStockQuantity += itemQty;
                    break;
                case TransactionUtil.cstInventoryShipment:
                    objInventoryStocksInfo.ICInventoryStockQuantity -= itemQty;
                    break;
                case TransactionUtil.cstInventoryAdjust:
                    objInventoryStocksInfo.ICInventoryStockQuantity = itemQty;
                    break;
            }

            ICProductsController objProductsController = new ICProductsController();
            ICProductsInfo objProductsInfo = (ICProductsInfo)objProductsController.GetObjectByID(productID);
            if (objProductsInfo != null)
            {
                if (objInventoryStocksInfo.ICInventoryStockQuantity - objInventoryStocksInfo.ICInventoryStockSaleOrderQuantity < 0)
                    return TransactionUtil.InventoryStatus.Empty;
                if (DateTime.Now.CompareTo(objProductsInfo.ICProductStockMinDateFrom) >= 0 && DateTime.Now.CompareTo(objProductsInfo.ICProductStockMinDateTo) <= 0)
                {
                    if (objInventoryStocksInfo.ICInventoryStockQuantity < objProductsInfo.ICProductStockMin)
                        return TransactionUtil.InventoryStatus.LessThanMinQty;
                }
                if (DateTime.Now.CompareTo(objProductsInfo.ICProductStockMaxDateFrom) >= 0 && DateTime.Now.CompareTo(objProductsInfo.ICProductStockMaxDateTo) <= 0)
                {
                    if (objInventoryStocksInfo.ICInventoryStockQuantity > objProductsInfo.ICProductStockMax)
                        return TransactionUtil.InventoryStatus.GreaterThanMaxQty;
                }
            }
            return TransactionUtil.InventoryStatus.Valid;
        }
    }
}
